/**
 * 字符截取
 * @param {sting} 字符串内容 
 * @param {length} 字符串截取长度
 * */
export const cutString = (string, length) => {
	if (!string || (string.length <= length)) {
		return string;
	}
	return string.substr(0, length) + '...';
}

/**
 * 转换日期格式
 * */
export const formatDate = (iniDate, format) => {
	if (!iniDate) {
		return ''
	} else if (isNaN(Date.parse(iniDate))) {
		iniDate = JSON.parse(iniDate)
	}
	var date = new Date(iniDate)
	var year = date.getFullYear()
	var month = doubleNumber(date.getMonth() + 1)
	var day = doubleNumber(date.getDate())
	var hour = doubleNumber(date.getHours())
	var minute = doubleNumber(date.getMinutes())
	var second = doubleNumber(date.getSeconds())

	switch (format) {
		case 'hh:mm':
			return hour + ':' + minute
		case 'hh:mm:ss':
			return hour + ':' + minute + ':' + second
		case 'yyyy-MM-dd':
			return year + '-' + month + '-' + day
		case 'yyyy.MM.dd':
			return year + '.' + month + '.' + day
		case 'yyyy-MM-dd hh:mm':
			return year + '-' + month + '-' + day + ' ' + hour + ':' + minute
		case 'yyyy-MM-dd hh:mm:ss':
			return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
		case 'yyyy/MM/dd hh:mm:ss':
			return year + '/' + month + '/' + day + ' ' + hour + ':' + minute + ':' + second
		case 'yyyy/MM/dd':
			return year + '/' + month + '/' + day
		case 'yyyy-M-d':
			return year + '-' + month + '-' + date.getDate()
		case 'yyyyMMdd':
			return year + month + day
		case 'hhmmss':
			return hour + '' + minute + '' + second
	}
}
//时分秒转时分显示
export const turnTime = time=>{
	return time.substr(0,5)
}
//两位数
export const doubleNumber = (number) => {
	return number < 10 ? '0' + number : number
}

//深拷贝
export const deepCopy = (obj) => {
	//判断拷贝的要进行深拷贝的是数组还是对象，是数组的话进行数组拷贝，对象的话进行对象拷贝
	var objClone = Array.isArray(obj) ? [] : {};
	//进行深拷贝的不能为空，并且是对象或者是
	if (obj && typeof obj === "object") {
		for (let key in obj) {
			if (obj.hasOwnProperty(key)) {
				if (obj[key] && typeof obj[key] === "object") {
					objClone[key] = deepCopy(obj[key]);
				} else {
					objClone[key] = obj[key];
				}
			}
		}
	}
	return objClone;
}

//枚举转数组
export const makeEnumToArray = (enumObject) => {
	var all = [];
	for (var key in enumObject) {
		all.push({
			key,
			name: enumObject[key]
		});
	}
	return all;
}
